<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta name="referrer" content="no-referrer"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板 | Welcome,Guys,Join Me for the AIminminAI Episode!</title><meta name="author" content="Hu hb"><meta name="copyright" content="Hu hb"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="@TOC  前言做项目的时候，用到了WPF，总结下来，供以后参考，与诸君共勉。 一、WPF中的复用精神：内联样式和模板 Inline styles and templates Windows Presentation Foundation (WPF) 提供了 Style 对象和模板对象（FrameworkTemplate 子类）作为定义资源中元素的视觉外观的方法，以便  用户可以多次使用它们 。">
<meta property="og:type" content="article">
<meta property="og:title" content="打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板">
<meta property="og:url" content="https://aiminminai.gitee.io/aiminminai/2024/02/28/%E6%89%93%E7%A0%B4%E7%A0%82%E9%94%85%E9%97%AE-WPF%E5%8E%9F%E7%90%86-03-WPF%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%92%8C%E6%A8%A1%E6%9D%BF%E5%8C%96%E3%80%81%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF/index.html">
<meta property="og:site_name" content="Welcome,Guys,Join Me for the AIminminAI Episode!">
<meta property="og:description" content="@TOC  前言做项目的时候，用到了WPF，总结下来，供以后参考，与诸君共勉。 一、WPF中的复用精神：内联样式和模板 Inline styles and templates Windows Presentation Foundation (WPF) 提供了 Style 对象和模板对象（FrameworkTemplate 子类）作为定义资源中元素的视觉外观的方法，以便  用户可以多次使用它们 。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://aiminminai.gitee.io/aiminminai/img/avatar.jpg">
<meta property="article:published_time" content="2024-02-28T15:10:28.000Z">
<meta property="article:modified_time" content="2024-02-29T15:15:16.942Z">
<meta property="article:author" content="Hu hb">
<meta property="article:tag" content="内联样式和模板 Inline styles and templates">
<meta property="article:tag" content="Windows Presentation Foundation (WPF) 样式设置和模板化">
<meta property="article:tag" content="一般的 Windows Presentation Foundation (WPF) 编程模型原则:将代码中的程序逻辑与标记中的设计分离">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://aiminminai.gitee.io/aiminminai/img/avatar.jpg"><link rel="shortcut icon" href="/aiminminai/img/favicon.png"><link rel="canonical" href="https://aiminminai.gitee.io/aiminminai/2024/02/28/%E6%89%93%E7%A0%B4%E7%A0%82%E9%94%85%E9%97%AE-WPF%E5%8E%9F%E7%90%86-03-WPF%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%92%8C%E6%A8%A1%E6%9D%BF%E5%8C%96%E3%80%81%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/aiminminai/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/aiminminai/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  dateSuffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: {"limitCount":50,"languages":{"author":"Author: Hu hb","link":"Link: ","source":"Source: Welcome,Guys,Join Me for the AIminminAI Episode!","info":"Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source."}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板',
  isPost: true,
  isHome: false,
  isHighlightShrink: true,
  isToc: true,
  postUpdate: '2024-02-29 23:15:16'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/aiminminai/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/aiminminai/archives/"><div class="headline">Articles</div><div class="length-num">29</div></a><a href="/aiminminai/tags/"><div class="headline">Tags</div><div class="length-num">47</div></a><a href="/aiminminai/categories/"><div class="headline">Categories</div><div class="length-num">42</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/aiminminai/"><i class="fa-fw fas fa-home"></i><span> Home(主页)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives(那些年的知识归档)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags(那些年的知识标签)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories(那些年的工作经验)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/link/"><i class="fa-fw fas fa-link"></i><span> Link(那些年的八股)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/game/"><i class="fa-fw fas fa-heart"></i><span> Game Area(游戏专区，学&amp;玩)</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/aiminminai/img/JingSeOne.jpg')"><nav id="nav"><span id="blog-info"><a href="/aiminminai/" title="Welcome,Guys,Join Me for the AIminminAI Episode!"><span class="site-name">Welcome,Guys,Join Me for the AIminminAI Episode!</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/aiminminai/"><i class="fa-fw fas fa-home"></i><span> Home(主页)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives(那些年的知识归档)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags(那些年的知识标签)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories(那些年的工作经验)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/link/"><i class="fa-fw fas fa-link"></i><span> Link(那些年的八股)</span></a></div><div class="menus_item"><a class="site-page" href="/aiminminai/game/"><i class="fa-fw fas fa-heart"></i><span> Game Area(游戏专区，学&amp;玩)</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2024-02-28T15:10:28.000Z" title="Created 2024-02-28 23:10:28">2024-02-28</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2024-02-29T15:15:16.942Z" title="Updated 2024-02-29 23:15:16">2024-02-29</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/aiminminai/categories/%E5%8E%9F%E7%90%86%E4%B8%8E%E6%BA%90%E7%A0%81/">原理与源码</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/aiminminai/categories/%E5%8E%9F%E7%90%86%E4%B8%8E%E6%BA%90%E7%A0%81/WPF%E5%8E%9F%E7%90%86%E6%8E%A2%E6%8E%A2%E6%8E%A2%EF%BC%88%E4%B8%80%EF%BC%89/">WPF原理探探探（一）</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/aiminminai/categories/%E5%8E%9F%E7%90%86%E4%B8%8E%E6%BA%90%E7%A0%81/WPF%E5%8E%9F%E7%90%86%E6%8E%A2%E6%8E%A2%E6%8E%A2%EF%BC%88%E4%B8%80%EF%BC%89/WPF%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%92%8C%E6%A8%A1%E6%9D%BF%E5%8C%96%E3%80%81%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF/">WPF样式设置和模板化、内联样式和模板</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>@<a href="%E6%96%87%E7%AB%A0%E7%9B%AE%E5%BD%95">TOC</a></p>
<hr>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>做项目的时候，用到了WPF，总结下来，供以后参考，与诸君共勉。</p>
<h1 id="一、WPF中的复用精神：内联样式和模板-Inline-styles-and-templates"><a href="#一、WPF中的复用精神：内联样式和模板-Inline-styles-and-templates" class="headerlink" title="一、WPF中的复用精神：内联样式和模板 Inline styles and templates"></a>一、WPF中的复用精神：内联样式和模板 Inline styles and templates</h1><ul>
<li>Windows Presentation Foundation (WPF) 提供了 Style 对象和模板对象（FrameworkTemplate 子类）作为定义资源中元素的视觉外观的方法，以便 <font color="red"> 用户可以多次使用它们 </font>。<ul>
<li>因此，<font color="red"> 采用 Style 和 FrameworkTemplate 类型的 XAML 中的属性几乎总是对现有样式和模板进行资源引用，而不是定义新的嵌入式样式和模板。 </font></li>
</ul>
</li>
</ul>
<h2 id="1-嵌入式样式和模板的限制-Limitations-of-embedded-styles-and-templates："><a href="#1-嵌入式样式和模板的限制-Limitations-of-embedded-styles-and-templates：" class="headerlink" title="1.嵌入式样式和模板的限制 Limitations of embedded styles and templates："></a>1.嵌入式样式和模板的限制 Limitations of embedded styles and templates：</h2><ul>
<li>在 Extensible Application Markup Language (XAML) 中，样式和模板属性在技术上可通过两种方式进行设置<ul>
<li>方式一：可以使用属性语法来引用已在资源中定义的样式<ul>
<li>如 &lt;objectStyle&#x3D;”{StaticResourcemyResourceKey}” …&#x2F;&gt;</li>
<li>定义为嵌入式且未在资源中定义的样式必须仅限于包含元素，由于没有资源键，因此不能轻易重用它</li>
</ul>
</li>
<li>方式二：可以使用属性元素语法来定义嵌入式样式<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs C#">&lt;<span class="hljs-built_in">object</span>&gt;<br>&lt;<span class="hljs-built_in">object</span>.Style&gt;<br>&lt; Style .../&gt;<br>对象<br>对象<br></code></pre></td></tr></table></figure>
<ul>
<li>可以将 Style 视为一种将一组属性值应用到多个元素的便捷方法， 可以对从 FrameworkElement 或 FrameworkContentElement（如 Window 或 Button）派生的任何元素使用样式。<ul>
<li><h2 id="声明样式的最常见方法是在-XAML-文件的-Resources-部分中声明为资源。-由于样式是一种资源，声明样式的位置会影响样式的应用范围。-例如，如果在应用定义-XAML-文件的根元素中声明样式，则该样式可以在应用中的任何位置使用。-例如，以下-XAML-代码为-TextBlock-声明了两个样式，一个自动应用于所有-TextBlock-元素，另一个则必须显式引用。"><a href="#声明样式的最常见方法是在-XAML-文件的-Resources-部分中声明为资源。-由于样式是一种资源，声明样式的位置会影响样式的应用范围。-例如，如果在应用定义-XAML-文件的根元素中声明样式，则该样式可以在应用中的任何位置使用。-例如，以下-XAML-代码为-TextBlock-声明了两个样式，一个自动应用于所有-TextBlock-元素，另一个则必须显式引用。" class="headerlink" title="声明样式的最常见方法是在 XAML 文件的 Resources 部分中声明为资源。 由于样式是一种资源，声明样式的位置会影响样式的应用范围。- 例如，如果在应用定义 XAML 文件的根元素中声明样式，则该样式可以在应用中的任何位置使用。- 例如，以下 XAML 代码为 TextBlock 声明了两个样式，一个自动应用于所有 TextBlock 元素，另一个则必须显式引用。"></a>声明样式的最常见方法是在 XAML 文件的 Resources 部分中声明为资源。 由于样式是一种资源，声明样式的位置会影响样式的应用范围。<br>- 例如，如果在应用定义 XAML 文件的根元素中声明样式，则该样式可以在应用中的任何位置使用。<br>- 例如，以下 XAML 代码为 TextBlock 声明了两个样式，一个自动应用于所有 TextBlock 元素，另一个则必须显式引用。<br><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs C#">&lt;Window.Resources&gt;<br>    &lt;!-- .... other resources .... --&gt;<br><br>    &lt;!--A Style that affects all TextBlocks--&gt;<br>    &lt;Style TargetType=<span class="hljs-string">&quot;TextBlock&quot;</span>&gt;<br>        &lt;Setter Property=<span class="hljs-string">&quot;HorizontalAlignment&quot;</span> Value=<span class="hljs-string">&quot;Center&quot;</span> /&gt;<br>        &lt;Setter Property=<span class="hljs-string">&quot;FontFamily&quot;</span> Value=<span class="hljs-string">&quot;Comic Sans MS&quot;</span>/&gt;<br>        &lt;Setter Property=<span class="hljs-string">&quot;FontSize&quot;</span> Value=<span class="hljs-string">&quot;14&quot;</span>/&gt;<br>    &lt;/Style&gt;<br>    <br>    &lt;!--A Style that extends the previous TextBlock Style <span class="hljs-keyword">with</span> an x:Key of TitleText--&gt;<br>    &lt;Style BasedOn=<span class="hljs-string">&quot;&#123;StaticResource &#123;x:Type TextBlock&#125;&#125;&quot;</span><br>        TargetType=<span class="hljs-string">&quot;TextBlock&quot;</span><br>        x:Key=<span class="hljs-string">&quot;TitleText&quot;</span>&gt;<br>        &lt;Setter Property=<span class="hljs-string">&quot;FontSize&quot;</span> Value=<span class="hljs-string">&quot;26&quot;</span>/&gt;<br>        &lt;Setter Property=<span class="hljs-string">&quot;Foreground&quot;</span>&gt;<br>            &lt;Setter.Value&gt;<br>                &lt;LinearGradientBrush StartPoint=<span class="hljs-string">&quot;0.5,0&quot;</span> EndPoint=<span class="hljs-string">&quot;0.5,1&quot;</span>&gt;<br>                    &lt;LinearGradientBrush.GradientStops&gt;<br>                        &lt;GradientStop Offset=<span class="hljs-string">&quot;0.0&quot;</span> Color=<span class="hljs-string">&quot;#90DDDD&quot;</span> /&gt;<br>                        &lt;GradientStop Offset=<span class="hljs-string">&quot;1.0&quot;</span> Color=<span class="hljs-string">&quot;#5BFFFF&quot;</span> /&gt;<br>                    &lt;/LinearGradientBrush.GradientStops&gt;<br>                &lt;/LinearGradientBrush&gt;<br>            &lt;/Setter.Value&gt;<br>        &lt;/Setter&gt;<br>    &lt;/Style&gt;<br>&lt;/Window.Resources&gt;<br><span class="hljs-comment">//如何使用上面声明的样式</span><br>&lt;StackPanel&gt;<br>    &lt;TextBlock Style=<span class="hljs-string">&quot;&#123;StaticResource TitleText&#125;&quot;</span> Name=<span class="hljs-string">&quot;textblock1&quot;</span>&gt;My Pictures&lt;/TextBlock&gt;<br>    &lt;TextBlock&gt;Check <span class="hljs-keyword">out</span> my <span class="hljs-keyword">new</span> pictures!&lt;/TextBlock&gt;<br>&lt;/StackPanel&gt;<br></code></pre></td></tr></table></figure></h2></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="2-一般的-Windows-Presentation-Foundation-WPF-编程模型原则"><a href="#2-一般的-Windows-Presentation-Foundation-WPF-编程模型原则" class="headerlink" title="2.一般的 Windows Presentation Foundation (WPF) 编程模型原则"></a>2.一般的 Windows Presentation Foundation (WPF) 编程模型原则</h2><ul>
<li>将代码中的程序逻辑与标记中的设计分离<ul>
<li><font color="red"> WPF 样式设置模型的另一项功能是将呈现与逻辑分离 </font><ul>
<li>设计者可以仅使用 XAML 处理应用外观，与此同时开发者使用 C# 或 Visual Basic 处理编程逻辑<ul>
<li>其实呢，感觉就是用XAML模仿静态三剑客【HTML、CSS、Jabascript】等，然后C#模仿Java全家桶玩前后端分离呗</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>Windows Presentation Foundation (WPF) 样式设置和模板化是指一套功能（样式、模板、触发器和情节提要），可使应用程序、文档和用户界面 (UI) 设计者创建极具视觉表现力的应用程序，并为其产品创建标准化的特定外观。</li>
</ul>
<h2 id="3-ControlTemplate"><a href="#3-ControlTemplate" class="headerlink" title="3.ControlTemplate"></a>3.ControlTemplate</h2><ul>
<li>在 WPF 中，控件的 ControlTemplate 用于定义控件的外观<ul>
<li>可以通过定义新的 ControlTemplate 并将其分配给控件来更改控件的结构和外观，从而无需自行编写自定义控件</li>
</ul>
</li>
<li>每个控件都有一个分配给 Control.Template 属性的默认模板。 该模板将控件的视觉呈现与控件的功能关联起来<ul>
<li>在 XAML 中定义了模板，设计器通常允许创建现有模板的副本并进行修改<ul>
<li>在 Visual Studio WPF 设计器中，选择一个 CheckBox 控件，然后右键单击并选择“编辑模板”&gt;“创建副本”，此命令会生成一个用于定义模板的样式</li>
</ul>
</li>
<li>控件模板是通过设置 Control.Template 属性来应用的，因此可以使用样式来定义或设置模板</li>
<li>通常在 XAML 文件的 Resources 部分中将模板声明为资源。<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs C#">&lt;Style x:Key=<span class="hljs-string">&quot;CheckBoxStyle1&quot;</span> TargetType=<span class="hljs-string">&quot;&#123;x:Type CheckBox&#125;&quot;</span>&gt;<br>    &lt;Setter Property=<span class="hljs-string">&quot;FocusVisualStyle&quot;</span> Value=<span class="hljs-string">&quot;&#123;StaticResource FocusVisual1&#125;&quot;</span>/&gt;<br>    &lt;Setter Property=<span class="hljs-string">&quot;Background&quot;</span> Value=<span class="hljs-string">&quot;&#123;StaticResource OptionMark.Static.Background1&#125;&quot;</span>/&gt;<br>    &lt;Setter Property=<span class="hljs-string">&quot;BorderBrush&quot;</span> Value=<span class="hljs-string">&quot;&#123;StaticResource OptionMark.Static.Border1&#125;&quot;</span>/&gt;<br>    &lt;Setter Property=<span class="hljs-string">&quot;Foreground&quot;</span> Value=<span class="hljs-string">&quot;&#123;DynamicResource &#123;x:Static SystemColors.ControlTextBrushKey&#125;&#125;&quot;</span>/&gt;<br>    &lt;Setter Property=<span class="hljs-string">&quot;BorderThickness&quot;</span> Value=<span class="hljs-string">&quot;1&quot;</span>/&gt;<br>    &lt;Setter Property=<span class="hljs-string">&quot;Template&quot;</span>&gt;<br>        &lt;Setter.Value&gt;<br>            &lt;ControlTemplate TargetType=<span class="hljs-string">&quot;&#123;x:Type CheckBox&#125;&quot;</span>&gt;<br>                &lt;Grid x:Name=<span class="hljs-string">&quot;templateRoot&quot;</span> Background=<span class="hljs-string">&quot;Transparent&quot;</span> SnapsToDevicePixels=<span class="hljs-string">&quot;True&quot;</span>&gt;<br>                    &lt;Grid.ColumnDefinitions&gt;<br>                        &lt;ColumnDefinition Width=<span class="hljs-string">&quot;Auto&quot;</span>/&gt;<br>                        &lt;ColumnDefinition Width=<span class="hljs-string">&quot;*&quot;</span>/&gt;<br>                    &lt;/Grid.ColumnDefinitions&gt;<br>                    &lt;Border x:Name=<span class="hljs-string">&quot;checkBoxBorder&quot;</span> Background=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span> BorderThickness=<span class="hljs-string">&quot;&#123;TemplateBinding BorderThickness&#125;&quot;</span> BorderBrush=<span class="hljs-string">&quot;&#123;TemplateBinding BorderBrush&#125;&quot;</span> HorizontalAlignment=<span class="hljs-string">&quot;&#123;TemplateBinding HorizontalContentAlignment&#125;&quot;</span> Margin=<span class="hljs-string">&quot;1&quot;</span> VerticalAlignment=<span class="hljs-string">&quot;&#123;TemplateBinding VerticalContentAlignment&#125;&quot;</span>&gt;<br>                        &lt;Grid x:Name=<span class="hljs-string">&quot;markGrid&quot;</span>&gt;<br>                            &lt;Path x:Name=<span class="hljs-string">&quot;optionMark&quot;</span> Data=<span class="hljs-string">&quot;F1 M 9.97498,1.22334L 4.6983,9.09834L 4.52164,9.09834L 0,5.19331L 1.27664,3.52165L 4.255,6.08833L 8.33331,1.52588e-005L 9.97498,1.22334 Z &quot;</span> Fill=<span class="hljs-string">&quot;&#123;StaticResource OptionMark.Static.Glyph1&#125;&quot;</span> Margin=<span class="hljs-string">&quot;1&quot;</span> Opacity=<span class="hljs-string">&quot;0&quot;</span> Stretch=<span class="hljs-string">&quot;None&quot;</span>/&gt;<br>                            &lt;Rectangle x:Name=<span class="hljs-string">&quot;indeterminateMark&quot;</span> Fill=<span class="hljs-string">&quot;&#123;StaticResource OptionMark.Static.Glyph1&#125;&quot;</span> Margin=<span class="hljs-string">&quot;2&quot;</span> Opacity=<span class="hljs-string">&quot;0&quot;</span>/&gt;<br>                        &lt;/Grid&gt;<br>                    &lt;/Border&gt;<br>                    &lt;ContentPresenter x:Name=<span class="hljs-string">&quot;contentPresenter&quot;</span> Grid.Column=<span class="hljs-string">&quot;1&quot;</span> Focusable=<span class="hljs-string">&quot;False&quot;</span> HorizontalAlignment=<span class="hljs-string">&quot;&#123;TemplateBinding HorizontalContentAlignment&#125;&quot;</span> Margin=<span class="hljs-string">&quot;&#123;TemplateBinding Padding&#125;&quot;</span> RecognizesAccessKey=<span class="hljs-string">&quot;True&quot;</span> SnapsToDevicePixels=<span class="hljs-string">&quot;&#123;TemplateBinding SnapsToDevicePixels&#125;&quot;</span> VerticalAlignment=<span class="hljs-string">&quot;&#123;TemplateBinding VerticalContentAlignment&#125;&quot;</span>/&gt;<br>                &lt;/Grid&gt;<br>                &lt;ControlTemplate.Triggers&gt;<br>                    &lt;Trigger Property=<span class="hljs-string">&quot;HasContent&quot;</span> Value=<span class="hljs-string">&quot;true&quot;</span>&gt;<br>                        &lt;Setter Property=<span class="hljs-string">&quot;FocusVisualStyle&quot;</span> Value=<span class="hljs-string">&quot;&#123;StaticResource OptionMarkFocusVisual1&#125;&quot;</span>/&gt;<br>                        &lt;Setter Property=<span class="hljs-string">&quot;Padding&quot;</span> Value=<span class="hljs-string">&quot;4,-1,0,0&quot;</span>/&gt;<br><br>... content removed to save space ...<br></code></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h2 id="4-TemplateBinding"><a href="#4-TemplateBinding" class="headerlink" title="4.TemplateBinding"></a>4.TemplateBinding</h2><ul>
<li>对于模板方案来说，TemplateBinding 是绑定的优化形式，类似于使用 {Binding RelativeSource&#x3D;{RelativeSource TemplatedParent}} 构造的绑定。 <ul>
<li>TemplateBinding 可用于将模板的各个部分绑定到控件的各个属性。<ul>
<li>例如，每个控件都有一个 BorderThickness 属性。 可使用 TemplateBinding 管理此控件设置影响模板中的哪个元素</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="5-ContentControl-and-ItemsControl"><a href="#5-ContentControl-and-ItemsControl" class="headerlink" title="5.ContentControl and ItemsControl"></a>5.ContentControl and ItemsControl</h2><ul>
<li>如果在 ContentControl 的 ControlTemplate 中声明了 ContentPresenter，ContentPresenter 将自动绑定到 ContentTemplate 和 Content 属性。<ul>
<li>ItemsControl 的 ControlTemplate 中的 ItemsPresenter 将自动绑定到 ItemTemplate 和 Items 属性。</li>
</ul>
</li>
</ul>
<h2 id="6-一个绑定到照片列表的-ListBox-控件。"><a href="#6-一个绑定到照片列表的-ListBox-控件。" class="headerlink" title="6.一个绑定到照片列表的 ListBox 控件。"></a>6.一个绑定到照片列表的 ListBox 控件。</h2><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs C#">&lt;ListBox ItemsSource=<span class="hljs-string">&quot;&#123;Binding Source=&#123;StaticResource MyPhotos&#125;&#125;&quot;</span><br>         Background=<span class="hljs-string">&quot;Silver&quot;</span> Width=<span class="hljs-string">&quot;600&quot;</span> Margin=<span class="hljs-string">&quot;10&quot;</span> SelectedIndex=<span class="hljs-string">&quot;0&quot;</span>/&gt;<br><br><span class="hljs-comment">//在此示例中，数据是照片列表</span><br><span class="hljs-comment">//每个自定义 Photo 对象都有一个字符串类型的 Source 属性，用于指定图像的文件路径。 当前，照片对象显示为文件路径。</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Photo</span><br>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Photo</span>(<span class="hljs-params"><span class="hljs-built_in">string</span> path</span>)</span><br>    &#123;<br>        Source = path;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-built_in">string</span> Source &#123; <span class="hljs-keyword">get</span>; &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> <span class="hljs-built_in">string</span> <span class="hljs-title">ToString</span>()</span> =&gt; Source;<br>&#125;<br><span class="hljs-comment">//若要使照片显示为图像，可以将 DataTemplate 创建为资源</span><br>&lt;Window.Resources&gt;<br>    &lt;!-- .... other resources .... --&gt;<br><br>    &lt;!--DataTemplate to display Photos <span class="hljs-keyword">as</span> images<br>    instead of text strings of Paths--&gt;<br>    &lt;DataTemplate DataType=<span class="hljs-string">&quot;&#123;x:Type local:Photo&#125;&quot;</span>&gt;<br>        &lt;Border Margin=<span class="hljs-string">&quot;3&quot;</span>&gt;<br>            &lt;Image Source=<span class="hljs-string">&quot;&#123;Binding Source&#125;&quot;</span>/&gt;<br>        &lt;/Border&gt;<br>    &lt;/DataTemplate&gt;<br>&lt;/Window.Resources&gt;<br><span class="hljs-comment">//DataTemplate 定义每当存在 Photo 对象时，它都应显示为 Border 内的 Image。</span><br></code></pre></td></tr></table></figure>
<ul>
<li>在 WPF 中，使用 DataTemplate 定义数据的视觉表示形式。 <ul>
<li>放入 DataTemplate 的内容决定了数据在呈现的应用中的外观</li>
</ul>
</li>
</ul>
<h2 id="7-PropertyTrigger"><a href="#7-PropertyTrigger" class="headerlink" title="7.PropertyTrigger"></a>7.PropertyTrigger</h2><ul>
<li>根据属性的值设置属性值或启动操作的 Trigger 称为属性触发器<ul>
<li>要演示如何使用属性触发器，可以使每个 ListBoxItem 在未选中时部分透明。 以下样式将 ListBoxItem 的 Opacity 值设置为 0.5。 但是，当 IsSelected 属性为 true 时，Opacity 设置为 1.0。<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs C#">&lt;Window.Resources&gt;<br>  &lt;!-- .... other resources .... --&gt;<br><br>  &lt;Style TargetType=<span class="hljs-string">&quot;ListBoxItem&quot;</span>&gt;<br>      &lt;Setter Property=<span class="hljs-string">&quot;Opacity&quot;</span> Value=<span class="hljs-string">&quot;0.5&quot;</span> /&gt;<br>      &lt;Setter Property=<span class="hljs-string">&quot;MaxHeight&quot;</span> Value=<span class="hljs-string">&quot;75&quot;</span> /&gt;<br>      &lt;Style.Triggers&gt;<br>          &lt;Trigger Property=<span class="hljs-string">&quot;IsSelected&quot;</span> Value=<span class="hljs-string">&quot;True&quot;</span>&gt;<br>              &lt;Trigger.Setters&gt;<br>                  &lt;Setter Property=<span class="hljs-string">&quot;Opacity&quot;</span> Value=<span class="hljs-string">&quot;1.0&quot;</span> /&gt;<br>              &lt;/Trigger.Setters&gt;<br>          &lt;/Trigger&gt;<br>      &lt;/Style.Triggers&gt;<br>  &lt;/Style&gt;<br>&lt;/Window.Resources&gt;<br></code></pre></td></tr></table></figure></li>
</ul>
</li>
<li>Trigger 类还有 EnterActions 和 ExitActions 属性，这些属性可使触发器执行操作。</li>
<li>另一个触发器类型是 EventTrigger，用于根据某个事件的发生启动一组操作。 <figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs C#"><span class="hljs-comment">// EventTrigger 对象指定当鼠标指针进入 ListBoxItem 时，MaxHeight 属性在 0.2 秒的时间内动画化为值 90。 当鼠标离开该项时，属性在 1 秒的时间内返回到原始值。 请注意，不需要为 MouseLeave 动画指定 To 值。 这是因为动画能够跟踪原始值。</span><br>&lt;Style.Triggers&gt;<br>    &lt;Trigger Property=<span class="hljs-string">&quot;IsSelected&quot;</span> Value=<span class="hljs-string">&quot;True&quot;</span>&gt;<br>        &lt;Trigger.Setters&gt;<br>            &lt;Setter Property=<span class="hljs-string">&quot;Opacity&quot;</span> Value=<span class="hljs-string">&quot;1.0&quot;</span> /&gt;<br>        &lt;/Trigger.Setters&gt;<br>    &lt;/Trigger&gt;<br>    &lt;EventTrigger RoutedEvent=<span class="hljs-string">&quot;Mouse.MouseEnter&quot;</span>&gt;<br>        &lt;EventTrigger.Actions&gt;<br>            &lt;BeginStoryboard&gt;<br>                &lt;Storyboard&gt;<br>                    &lt;DoubleAnimation<br>                        Duration=<span class="hljs-string">&quot;0:0:0.2&quot;</span><br>                        Storyboard.TargetProperty=<span class="hljs-string">&quot;MaxHeight&quot;</span><br>                        To=<span class="hljs-string">&quot;90&quot;</span>  /&gt;<br>                &lt;/Storyboard&gt;<br>            &lt;/BeginStoryboard&gt;<br>        &lt;/EventTrigger.Actions&gt;<br>    &lt;/EventTrigger&gt;<br>    &lt;EventTrigger RoutedEvent=<span class="hljs-string">&quot;Mouse.MouseLeave&quot;</span>&gt;<br>        &lt;EventTrigger.Actions&gt;<br>            &lt;BeginStoryboard&gt;<br>                &lt;Storyboard&gt;<br>                    &lt;DoubleAnimation<br>                        Duration=<span class="hljs-string">&quot;0:0:1&quot;</span><br>                        Storyboard.TargetProperty=<span class="hljs-string">&quot;MaxHeight&quot;</span>  /&gt;<br>                &lt;/Storyboard&gt;<br>            &lt;/BeginStoryboard&gt;<br>        &lt;/EventTrigger.Actions&gt;<br>    &lt;/EventTrigger&gt;<br>&lt;/Style.Triggers&gt;<br></code></pre></td></tr></table></figure></li>
</ul>
<h2 id="8-视觉状态-visual-status"><a href="#8-视觉状态-visual-status" class="headerlink" title="8.视觉状态 visual status"></a>8.视觉状态 visual status</h2><ul>
<li>大多数控件都有两个状态组：CommonStates 和 FocusStates。<ul>
<li>在应用于控件的每个状态组中，控件始终处于每个组的一种状态，例如 CommonStates.MouseOver 和 FocusStates.Unfocused。 但是，控件不能处于同一组中的两种不同状态</li>
</ul>
</li>
<li>控件始终处于特定的状态<ul>
<li>例如，当鼠标在控件的表面上移动时，该控件被视为处于公用状态 MouseOver。 没有特定状态的控件被视为处于公用 Normal 状态。</li>
</ul>
</li>
</ul>
<h1 id="二、例子：为-Button-控件创建新的-ControlTemplate"><a href="#二、例子：为-Button-控件创建新的-ControlTemplate" class="headerlink" title="二、例子：为 Button 控件创建新的 ControlTemplate"></a>二、例子：为 Button 控件创建新的 ControlTemplate</h1><ul>
<li>例子效果：<ul>
<li>一个典型的C#标题框，两个按钮：<ul>
<li>一个名为Unstyled Button，的按钮，按钮上写Button 1</li>
<li>一个名为Rounded Button，的按钮，按钮上写Button 2</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="1-何时创建-ControlTemplate-When-to-create-ControlTemplate：要自定义设置控件中其他属性无法实现的控件外观时，则创建-ControlTemplate"><a href="#1-何时创建-ControlTemplate-When-to-create-ControlTemplate：要自定义设置控件中其他属性无法实现的控件外观时，则创建-ControlTemplate" class="headerlink" title="1.何时创建 ControlTemplate When to create ControlTemplate：要自定义设置控件中其他属性无法实现的控件外观时，则创建 ControlTemplate"></a>1.何时创建 ControlTemplate When to create ControlTemplate：要自定义设置控件中其他属性无法实现的控件外观时，则创建 ControlTemplate</h2><ul>
<li>控件有许多属性，例如 Background、Foreground 和 FontFamily。 这些属性控制控件外观的不同方面，但可通过设置这些属性进行的更改有限。 例如，可以从 CheckBox 中将 Foreground 属性设置为蓝色，并将 FontStyle 设置为斜体。<ul>
<li>要自定义设置控件中其他属性无法实现的控件外观时，则创建 ControlTemplate。比如，<font color="red"> 在多数用户界面中，按钮的总体外观相同：即一个包含某些文本的矩形。 若想要创建一个圆形的按钮，可以创建一个继承自该按钮或重新创建该按钮功能的新控件。 此外，新用户控件还会提供圆形视觉对象。 </font><ul>
<li>通过自定义现有控件的可视布局，可以避免创建新控件。 借助圆形按钮，可创建具有所需可视布局的 ControlTemplate。</li>
<li>另一方面，如果你需要具有新功能、其他属性和新设置的控件，可创建新的 UserControl。</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="2-创建新的-WPF-应用程序，在-MainWindow-xaml（或选择的其他窗口）的-元素中设置以下属性"><a href="#2-创建新的-WPF-应用程序，在-MainWindow-xaml（或选择的其他窗口）的-元素中设置以下属性" class="headerlink" title="2.创建新的 WPF 应用程序，在 MainWindow.xaml（或选择的其他窗口）的  元素中设置以下属性"></a>2.创建新的 WPF 应用程序，在 MainWindow.xaml（或选择的其他窗口）的 <Window> 元素中设置以下属性</Window></h2><ul>
<li>Title<ul>
<li>如：Template Intro Sample</li>
</ul>
</li>
<li>SizeToContent<ul>
<li>如：WidthAndHeight</li>
</ul>
</li>
<li>MinWidth<ul>
<li>如：250</li>
</ul>
</li>
</ul>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs C#"><span class="hljs-comment">//将 &lt;Window&gt; 元素的内容设置为以下 XAML：</span><br>&lt;StackPanel Margin=<span class="hljs-string">&quot;10&quot;</span>&gt;<br>    &lt;Label&gt;Unstyled Button&lt;/Label&gt;<br>    &lt;Button&gt;Button <span class="hljs-number">1</span>&lt;/Button&gt;<br>    &lt;Label&gt;Rounded Button&lt;/Label&gt;<br>    &lt;Button&gt;Button <span class="hljs-number">2</span>&lt;/Button&gt;<br>&lt;/StackPanel&gt;<br></code></pre></td></tr></table></figure>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs C#"><span class="hljs-comment">//MainWindow.xaml 文件</span><br>&lt;Window x:Class=<span class="hljs-string">&quot;IntroToStylingAndTemplating.Window1&quot;</span><br>        xmlns=<span class="hljs-string">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span><br>        xmlns:x=<span class="hljs-string">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span><br>        xmlns:d=<span class="hljs-string">&quot;http://schemas.microsoft.com/expression/blend/2008&quot;</span><br>        xmlns:mc=<span class="hljs-string">&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span><br>        xmlns:local=<span class="hljs-string">&quot;clr-namespace:IntroToStylingAndTemplating&quot;</span><br>        mc:Ignorable=<span class="hljs-string">&quot;d&quot;</span><br>        Title=<span class="hljs-string">&quot;Template Intro Sample&quot;</span> SizeToContent=<span class="hljs-string">&quot;WidthAndHeight&quot;</span> MinWidth=<span class="hljs-string">&quot;250&quot;</span>&gt;<br>    &lt;StackPanel Margin=<span class="hljs-string">&quot;10&quot;</span>&gt;<br>        &lt;Label&gt;Unstyled Button&lt;/Label&gt;<br>        &lt;Button&gt;Button <span class="hljs-number">1</span>&lt;/Button&gt;<br>        &lt;Label&gt;Rounded Button&lt;/Label&gt;<br>        &lt;Button&gt;Button <span class="hljs-number">2</span>&lt;/Button&gt;<br>    &lt;/StackPanel&gt;<br>&lt;/Window&gt;<br></code></pre></td></tr></table></figure>

<h2 id="3-创建-ControlTemplate"><a href="#3-创建-ControlTemplate" class="headerlink" title="3.创建 ControlTemplate"></a>3.创建 ControlTemplate</h2><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs C#"><span class="hljs-comment">//为 Button 类创建一个简单的 ControlTemplate：</span><br>&lt;Style TargetType=<span class="hljs-string">&quot;&#123;x:Type Button&#125;&quot;</span>&gt;<br>  &lt;Setter Property=<span class="hljs-string">&quot;Template&quot;</span>&gt;<br>    &lt;Setter.Value&gt;<br>      &lt;ControlTemplate TargetType=<span class="hljs-string">&quot;&#123;x:Type Button&#125;&quot;</span>&gt;<br>        &lt;Grid Margin=<span class="hljs-string">&quot;5&quot;</span> Name=<span class="hljs-string">&quot;grid&quot;</span>&gt;<br>          &lt;Ellipse Stroke=<span class="hljs-string">&quot;DarkBlue&quot;</span> StrokeThickness=<span class="hljs-string">&quot;2&quot;</span>&gt;<br>            &lt;Ellipse.Fill&gt;<br>              &lt;RadialGradientBrush Center=<span class="hljs-string">&quot;0.3,0.2&quot;</span> RadiusX=<span class="hljs-string">&quot;0.5&quot;</span> RadiusY=<span class="hljs-string">&quot;0.5&quot;</span>&gt;<br>                &lt;GradientStop Color=<span class="hljs-string">&quot;Azure&quot;</span> Offset=<span class="hljs-string">&quot;0.1&quot;</span> /&gt;<br>                &lt;GradientStop Color=<span class="hljs-string">&quot;CornflowerBlue&quot;</span> Offset=<span class="hljs-string">&quot;1.1&quot;</span> /&gt;<br>              &lt;/RadialGradientBrush&gt;<br>            &lt;/Ellipse.Fill&gt;<br>          &lt;/Ellipse&gt;<br>          &lt;ContentPresenter Name=<span class="hljs-string">&quot;content&quot;</span> Margin=<span class="hljs-string">&quot;10&quot;</span><br>                            HorizontalAlignment=<span class="hljs-string">&quot;Center&quot;</span> VerticalAlignment=<span class="hljs-string">&quot;Center&quot;</span>/&gt;<br>        &lt;/Grid&gt;<br>      &lt;/ControlTemplate&gt;<br>    &lt;/Setter.Value&gt;<br>  &lt;/Setter&gt;<br>&lt;/Style&gt;<br></code></pre></td></tr></table></figure>
<ul>
<li><font color="red"> 声明 ControlTemplate 的最常见方法是在 XAML 文件的 Resources 部分中声明为资源。 </font><ul>
<li>如果在应用程序定义 XAML 文件的根元素中声明模板，则该模板可以在应用程序中的任何位置使用。 如果在窗口中定义模板，则仅该窗口中的控件可以使用该模板。<ul>
<li>模板是资源，因此它们遵从适用于所有资源的相同范围规则。 简言之，声明模板的位置会影响模板的应用范围</li>
</ul>
</li>
</ul>
</li>
<li>步骤一：将 Window.Resources 元素添加到 MainWindow.xaml 文件<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs C#">&lt;Window x:Class=<span class="hljs-string">&quot;IntroToStylingAndTemplating.Window2&quot;</span><br>        xmlns=<span class="hljs-string">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span><br>        xmlns:x=<span class="hljs-string">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span><br>        xmlns:d=<span class="hljs-string">&quot;http://schemas.microsoft.com/expression/blend/2008&quot;</span><br>        xmlns:mc=<span class="hljs-string">&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span><br>        xmlns:local=<span class="hljs-string">&quot;clr-namespace:IntroToStylingAndTemplating&quot;</span><br>        mc:Ignorable=<span class="hljs-string">&quot;d&quot;</span><br>        Title=<span class="hljs-string">&quot;Template Intro Sample&quot;</span> SizeToContent=<span class="hljs-string">&quot;WidthAndHeight&quot;</span> MinWidth=<span class="hljs-string">&quot;250&quot;</span>&gt;<br>    &lt;Window.Resources&gt;<br>        <br>    &lt;/Window.Resources&gt;<br>    &lt;StackPanel Margin=<span class="hljs-string">&quot;10&quot;</span>&gt;<br>        &lt;Label&gt;Unstyled Button&lt;/Label&gt;<br>        &lt;Button&gt;Button <span class="hljs-number">1</span>&lt;/Button&gt;<br>        &lt;Label&gt;Rounded Button&lt;/Label&gt;<br>        &lt;Button&gt;Button <span class="hljs-number">2</span>&lt;/Button&gt;<br>    &lt;/StackPanel&gt;<br>&lt;/Window&gt;<br></code></pre></td></tr></table></figure></li>
<li>步骤二：使用以下属性集创建新的 <ControlTemplate>：<ul>
<li>x:Key<ul>
<li>roundbutton</li>
</ul>
</li>
<li>TargetType<ul>
<li>Button<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs C#"><span class="hljs-comment">//控件的根元素 Grid</span><br><span class="hljs-comment">//用于绘制按钮圆形外观的 Ellipse。&lt;Ellipse&gt; 元素的 Fill 和 Stroke 属性绑定到了控件的 Foreground 和 Background 属性。</span><br><span class="hljs-comment">//用于显示用户指定的按钮内容的 ContentPresenter</span><br>&lt;ControlTemplate x:Key=<span class="hljs-string">&quot;roundbutton&quot;</span> TargetType=<span class="hljs-string">&quot;Button&quot;</span>&gt;<br>  &lt;Grid&gt;<br>      &lt;Ellipse Fill=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span> Stroke=<span class="hljs-string">&quot;&#123;TemplateBinding Foreground&#125;&quot;</span> /&gt;<br>      &lt;ContentPresenter HorizontalAlignment=<span class="hljs-string">&quot;Center&quot;</span> VerticalAlignment=<span class="hljs-string">&quot;Center&quot;</span> /&gt;<br>  &lt;/Grid&gt;<br>&lt;/ControlTemplate&gt;<br></code></pre></td></tr></table></figure></li>
</ul>
</li>
<li>步骤三：创建新的 ControlTemplate 时，可能仍然想要使用公共属性更改控件外观。 TemplateBinding 标记扩展将 ControlTemplate 中元素的属性绑定到由控件定义的公共属性。<ul>
<li>设置控件属性后，该值将传递到包含 TemplateBinding 的元素。</li>
<li>还将 <ContentPresenter> 元素添加到了模板。 此模板专为按钮设计，因此请注意该按钮继承自 ContentControl。 此按钮会显示该元素的内容。 可以在该按钮中设置任何内容，例如纯文本，甚至其他控件。 以下两个按钮均有效：<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs C#">&lt;Button&gt;My Text&lt;/Button&gt;<br><br>&lt;!-- <span class="hljs-keyword">and</span> --&gt;<br><br>&lt;Button&gt;<br>    &lt;CheckBox&gt;Checkbox <span class="hljs-keyword">in</span> a button&lt;/CheckBox&gt;<br>&lt;/Button&gt;<br></code></pre></td></tr></table></figure>
<ul>
<li>若将 ControlTemplate 应用到 ContentControl 类型（例如 Button），将在元素树中搜索 ContentPresenter。 若找到了 ContentPresenter，模板会自动将控件的 Content 属性绑定到 ContentPresenter。</li>
</ul>
</ContentPresenter></li>
</ul>
</li>
<li>步骤四：<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><code class="hljs C#">&lt;StackPanel Margin=<span class="hljs-string">&quot;10&quot;</span>&gt;<br>  &lt;Label&gt;Unstyled Button&lt;/Label&gt;<br>  &lt;Button&gt;Button <span class="hljs-number">1</span>&lt;/Button&gt;<br>  &lt;Label&gt;Rounded Button&lt;/Label&gt;<br>  &lt;Button&gt;Button <span class="hljs-number">2</span>&lt;/Button&gt;<br>&lt;/StackPanel&gt;<br><span class="hljs-comment">//将第二个按钮的 Template 属性设置为 roundbutton 资源：将看到此按钮具有圆形背景.此按钮不是一个圆形，而是倾斜的</span><br>&lt;StackPanel Margin=<span class="hljs-string">&quot;10&quot;</span>&gt;<br>  &lt;Label&gt;Unstyled Button&lt;/Label&gt;<br>  &lt;Button&gt;Button <span class="hljs-number">1</span>&lt;/Button&gt;<br>  &lt;Label&gt;Rounded Button&lt;/Label&gt;<br>  &lt;Button Template=<span class="hljs-string">&quot;&#123;StaticResource roundbutton&#125;&quot;</span>&gt;Button <span class="hljs-number">2</span>&lt;/Button&gt;<br>&lt;/StackPanel&gt;<br><span class="hljs-comment">//由于 &lt;Ellipse&gt; 元素的工作方式，它始终会扩展并填充可用空间。 将此按钮的 width 和 height 属性更改为同一个值，以使圆形均衡：</span><br>&lt;StackPanel Margin=<span class="hljs-string">&quot;10&quot;</span>&gt;<br>  &lt;Label&gt;Unstyled Button&lt;/Label&gt;<br>  &lt;Button&gt;Button <span class="hljs-number">1</span>&lt;/Button&gt;<br>  &lt;Label&gt;Rounded Button&lt;/Label&gt;<br>  &lt;Button Template=<span class="hljs-string">&quot;&#123;StaticResource roundbutton&#125;&quot;</span> Width=<span class="hljs-string">&quot;65&quot;</span> Height=<span class="hljs-string">&quot;65&quot;</span>&gt;Button <span class="hljs-number">2</span>&lt;/Button&gt;<br>&lt;/StackPanel&gt;<br><span class="hljs-comment">//监视按钮的 IsMouseOver 属性。 当鼠标位于控件上方时，使用新颜色设置 &lt;Ellipse&gt; 的样式。 此触发器类型称为 PropertyTrigger。必须为 &lt;Ellipse&gt; 添加一个可引用的名称，以便于触发器起作用。 将其命名为“backgroundElement”。</span><br>&lt;Ellipse x:Name=<span class="hljs-string">&quot;backgroundElement&quot;</span> Fill=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span> Stroke=<span class="hljs-string">&quot;&#123;TemplateBinding Foreground&#125;&quot;</span> /&gt;<br><span class="hljs-comment">//将新的 Trigger 添加到 ControlTemplate.Triggers 集合。 此触发器将监视 IsMouseOver 事件是否为值 true。</span><br>&lt;ControlTemplate x:Key=<span class="hljs-string">&quot;roundbutton&quot;</span> TargetType=<span class="hljs-string">&quot;Button&quot;</span>&gt;<br>  &lt;Grid&gt;<br>      &lt;Ellipse x:Name=<span class="hljs-string">&quot;backgroundElement&quot;</span> Fill=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span> Stroke=<span class="hljs-string">&quot;&#123;TemplateBinding Foreground&#125;&quot;</span> /&gt;<br>      &lt;ContentPresenter HorizontalAlignment=<span class="hljs-string">&quot;Center&quot;</span> VerticalAlignment=<span class="hljs-string">&quot;Center&quot;</span> /&gt;<br>  &lt;/Grid&gt;<br>  &lt;ControlTemplate.Triggers&gt;<br>      &lt;Trigger Property=<span class="hljs-string">&quot;IsMouseOver&quot;</span> Value=<span class="hljs-string">&quot;true&quot;</span>&gt;<br><br>      &lt;/Trigger&gt;<br>  &lt;/ControlTemplate.Triggers&gt;<br>&lt;/ControlTemplate&gt;<br><span class="hljs-comment">//将 &lt;Setter&gt; 添加到 &lt;Trigger&gt;，后者会将 &lt;Ellipse&gt; 的 Fill 属性更改为一种新颜色</span><br>&lt;Trigger Property=<span class="hljs-string">&quot;IsMouseOver&quot;</span> Value=<span class="hljs-string">&quot;true&quot;</span>&gt;<br>  &lt;Setter Property=<span class="hljs-string">&quot;Fill&quot;</span> TargetName=<span class="hljs-string">&quot;backgroundElement&quot;</span> Value=<span class="hljs-string">&quot;AliceBlue&quot;</span>/&gt;<br>&lt;/Trigger&gt;<br><span class="hljs-comment">//若要将 &lt;PropertyTrigger&gt; 转换为动画效果的可视状态，首先要从模板删除 &lt;ControlTemplate.Triggers&gt; 元素</span><br>&lt;ControlTemplate x:Key=<span class="hljs-string">&quot;roundbutton&quot;</span> TargetType=<span class="hljs-string">&quot;Button&quot;</span>&gt;<br>  &lt;Grid&gt;<br>      &lt;Ellipse x:Name=<span class="hljs-string">&quot;backgroundElement&quot;</span> Fill=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span> Stroke=<span class="hljs-string">&quot;&#123;TemplateBinding Foreground&#125;&quot;</span> /&gt;<br>      &lt;ContentPresenter HorizontalAlignment=<span class="hljs-string">&quot;Center&quot;</span> VerticalAlignment=<span class="hljs-string">&quot;Center&quot;</span> /&gt;<br>  &lt;/Grid&gt;<br>&lt;/ControlTemplate&gt;<br><span class="hljs-comment">//在控件模板的 &lt;Grid&gt; 根中，添加 &lt;VisualStateManager.VisualStateGroups&gt;，其中包含 CommonStates 的 &lt;VisualStateGroup&gt;。 定义两种状态：Normal 和 MouseOver。</span><br>&lt;ControlTemplate x:Key=<span class="hljs-string">&quot;roundbutton&quot;</span> TargetType=<span class="hljs-string">&quot;Button&quot;</span>&gt;<br>  &lt;Grid&gt;<br>      &lt;VisualStateManager.VisualStateGroups&gt;<br>          &lt;VisualStateGroup Name=<span class="hljs-string">&quot;CommonStates&quot;</span>&gt;<br>              &lt;VisualState Name=<span class="hljs-string">&quot;Normal&quot;</span>&gt;<br>              &lt;/VisualState&gt;<br>              &lt;VisualState Name=<span class="hljs-string">&quot;MouseOver&quot;</span>&gt;<br>              &lt;/VisualState&gt;<br>          &lt;/VisualStateGroup&gt;<br>      &lt;/VisualStateManager.VisualStateGroups&gt;<br>      &lt;Ellipse x:Name=<span class="hljs-string">&quot;backgroundElement&quot;</span> Fill=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span> Stroke=<span class="hljs-string">&quot;&#123;TemplateBinding Foreground&#125;&quot;</span> /&gt;<br>      &lt;ContentPresenter HorizontalAlignment=<span class="hljs-string">&quot;Center&quot;</span> VerticalAlignment=<span class="hljs-string">&quot;Center&quot;</span> /&gt;<br>  &lt;/Grid&gt;<br>&lt;/ControlTemplate&gt;<br><span class="hljs-comment">//触发 &lt;VisualState&gt; 时，将应用该状态中定义的任何动画。 为每种状态创建动画。 动画位于 &lt;Storyboard&gt; 元素中</span><br><span class="hljs-comment">//此状态对椭圆填充进行动画处理，将其还原为控件的 Background 颜色。</span><br>&lt;Storyboard&gt;<br>  &lt;ColorAnimation Storyboard.TargetName=<span class="hljs-string">&quot;backgroundElement&quot;</span> <br>      Storyboard.TargetProperty=<span class="hljs-string">&quot;(Shape.Fill).(SolidColorBrush.Color)&quot;</span><br>      To=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span><br>      Duration=<span class="hljs-string">&quot;0:0:0.3&quot;</span>/&gt;<br>&lt;/Storyboard&gt;<br><span class="hljs-comment">//MouseOver,此状态对椭圆 Background 颜色进行动画处理，将其更改为新颜色 Yellow。</span><br>&lt;Storyboard&gt;<br>  &lt;ColorAnimation Storyboard.TargetName=<span class="hljs-string">&quot;backgroundElement&quot;</span> <br>      Storyboard.TargetProperty=<span class="hljs-string">&quot;(Shape.Fill).(SolidColorBrush.Color)&quot;</span> <br>      To=<span class="hljs-string">&quot;Yellow&quot;</span> <br>      Duration=<span class="hljs-string">&quot;0:0:0.3&quot;</span>/&gt;<br>&lt;/Storyboard&gt;<br>&lt;ControlTemplate&gt; 如下所示:将鼠标移到按钮上方时，&lt;Ellipse&gt; 的颜色会进行动画处理。<br>&lt;ControlTemplate x:Key=<span class="hljs-string">&quot;roundbutton&quot;</span> TargetType=<span class="hljs-string">&quot;Button&quot;</span>&gt;<br>  &lt;Grid&gt;<br>      &lt;VisualStateManager.VisualStateGroups&gt;<br>          &lt;VisualStateGroup Name=<span class="hljs-string">&quot;CommonStates&quot;</span>&gt;<br>              &lt;VisualState Name=<span class="hljs-string">&quot;Normal&quot;</span>&gt;<br>                  &lt;Storyboard&gt;<br>                      &lt;ColorAnimation Storyboard.TargetName=<span class="hljs-string">&quot;backgroundElement&quot;</span> <br>                          Storyboard.TargetProperty=<span class="hljs-string">&quot;(Shape.Fill).(SolidColorBrush.Color)&quot;</span><br>                          To=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span><br>                          Duration=<span class="hljs-string">&quot;0:0:0.3&quot;</span>/&gt;<br>                  &lt;/Storyboard&gt;<br>              &lt;/VisualState&gt;<br>              &lt;VisualState Name=<span class="hljs-string">&quot;MouseOver&quot;</span>&gt;<br>                  &lt;Storyboard&gt;<br>                      &lt;ColorAnimation Storyboard.TargetName=<span class="hljs-string">&quot;backgroundElement&quot;</span> <br>                          Storyboard.TargetProperty=<span class="hljs-string">&quot;(Shape.Fill).(SolidColorBrush.Color)&quot;</span> <br>                          To=<span class="hljs-string">&quot;Yellow&quot;</span> <br>                          Duration=<span class="hljs-string">&quot;0:0:0.3&quot;</span>/&gt;<br>                  &lt;/Storyboard&gt;<br>              &lt;/VisualState&gt;<br>          &lt;/VisualStateGroup&gt;<br>      &lt;/VisualStateManager.VisualStateGroups&gt;<br>      &lt;Ellipse Name=<span class="hljs-string">&quot;backgroundElement&quot;</span> Fill=<span class="hljs-string">&quot;&#123;TemplateBinding Background&#125;&quot;</span> Stroke=<span class="hljs-string">&quot;&#123;TemplateBinding Foreground&#125;&quot;</span> /&gt;<br>      &lt;ContentPresenter x:Name=<span class="hljs-string">&quot;contentPresenter&quot;</span> HorizontalAlignment=<span class="hljs-string">&quot;Center&quot;</span> VerticalAlignment=<span class="hljs-string">&quot;Center&quot;</span> /&gt;<br>  &lt;/Grid&gt;<br>&lt;/ControlTemplate&gt;<br></code></pre></td></tr></table></figure></li>
</ul>
</ControlTemplate></li>
</ul>
<h1 id="三、Windows-Presentation-Foundation-WPF-数据绑定功能"><a href="#三、Windows-Presentation-Foundation-WPF-数据绑定功能" class="headerlink" title="三、Windows Presentation Foundation (WPF) 数据绑定功能"></a>三、Windows Presentation Foundation (WPF) 数据绑定功能</h1><p>……开个头，继续看</p>
<hr>
<h1 id="巨人的肩膀"><a href="#巨人的肩膀" class="headerlink" title="巨人的肩膀"></a>巨人的肩膀</h1><ul>
<li><a target="_blank" rel="noopener" href="https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/advanced/?view=netframeworkdesktop-4.8">https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/advanced/?view=netframeworkdesktop-4.8</a></li>
<li><a target="_blank" rel="noopener" href="https://cloud.tencent.com/developer/article/1342886">https://cloud.tencent.com/developer/article/1342886</a></li>
<li>官方手册：<a target="_blank" rel="noopener" href="https://learn.microsoft.com/zh-tw/dotnet/desktop/wpf/advanced/wpf-architecture?view=netframeworkdesktop-4.8">https://learn.microsoft.com/zh-tw/dotnet/desktop/wpf/advanced/wpf-architecture?view=netframeworkdesktop-4.8</a></li>
<li>Head First 设计模式</li>
<li>设计模式之禅</li>
<li>公众号啦、OSCHINA啦、上面的有关设计模式的文章</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://aiminminai.gitee.io/aiminminai">Hu hb</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://aiminminai.gitee.io/aiminminai/2024/02/28/%E6%89%93%E7%A0%B4%E7%A0%82%E9%94%85%E9%97%AE-WPF%E5%8E%9F%E7%90%86-03-WPF%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%92%8C%E6%A8%A1%E6%9D%BF%E5%8C%96%E3%80%81%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF/">https://aiminminai.gitee.io/aiminminai/2024/02/28/%E6%89%93%E7%A0%B4%E7%A0%82%E9%94%85%E9%97%AE-WPF%E5%8E%9F%E7%90%86-03-WPF%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%92%8C%E6%A8%A1%E6%9D%BF%E5%8C%96%E3%80%81%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/aiminminai/tags/%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF-Inline-styles-and-templates/">内联样式和模板 Inline styles and templates</a><a class="post-meta__tags" href="/aiminminai/tags/Windows-Presentation-Foundation-WPF-%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%92%8C%E6%A8%A1%E6%9D%BF%E5%8C%96/">Windows Presentation Foundation (WPF) 样式设置和模板化</a><a class="post-meta__tags" href="/aiminminai/tags/%E4%B8%80%E8%88%AC%E7%9A%84-Windows-Presentation-Foundation-WPF-%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%E5%8E%9F%E5%88%99-%E5%B0%86%E4%BB%A3%E7%A0%81%E4%B8%AD%E7%9A%84%E7%A8%8B%E5%BA%8F%E9%80%BB%E8%BE%91%E4%B8%8E%E6%A0%87%E8%AE%B0%E4%B8%AD%E7%9A%84%E8%AE%BE%E8%AE%A1%E5%88%86%E7%A6%BB/">一般的 Windows Presentation Foundation (WPF) 编程模型原则:将代码中的程序逻辑与标记中的设计分离</a></div><div class="post_share"><div class="social-share" data-image="/aiminminai/img/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> Donate</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/aiminminai/img/WeChat.jpg" target="_blank"><img class="post-qr-code-img" src="/aiminminai/img/WeChat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/aiminminai/img/Alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/aiminminai/img/Alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/aiminminai/2024/03/15/%E8%83%BD%E4%B8%8D%E8%83%BD%E7%BB%99%E5%88%AB%E4%BA%BA%E4%B8%80%E4%B8%AA%E4%B8%8A%E6%89%8B%E5%B0%B1%E8%83%BD%E7%9C%8B%E6%87%82%E5%B0%B1%E8%83%BD%E7%94%A8%E7%9A%84%E4%B8%89%E5%B1%82%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%EF%BC%88%E4%BB%A3%E7%A0%81%EF%BC%89%E6%A8%A1%E5%9E%8B%E5%91%A2%EF%BC%9F/" title="能不能给别人一个上手就能看懂就能用的三层神经网络（代码）模型呢？"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">能不能给别人一个上手就能看懂就能用的三层神经网络（代码）模型呢？</div></div></a></div><div class="next-post pull-right"><a href="/aiminminai/2024/02/27/%E6%89%93%E7%A0%B4%E7%A0%82%E9%94%85%E9%97%AE-WPF%E5%8E%9F%E7%90%86-02-WPF%E4%B8%AD%E7%9A%84XAML/" title="打破砂锅问-WPF原理-02-WPF中的XAML"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">打破砂锅问-WPF原理-02-WPF中的XAML</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/aiminminai/img/avatar.jpg" onerror="this.onerror=null;this.src='/aiminminai/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Hu hb</div><div class="author-info__description">寻寻觅觅，原来未来的你，在这里呀</div></div><div class="card-info-data site-data is-center"><a href="/aiminminai/archives/"><div class="headline">Articles</div><div class="length-num">29</div></a><a href="/aiminminai/tags/"><div class="headline">Tags</div><div class="length-num">47</div></a><a href="/aiminminai/categories/"><div class="headline">Categories</div><div class="length-num">42</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/aiminminai"><i class="Gitee"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:min221forever@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://github.com/YWMHHBAIminminAI" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/m0_52436398?type=blog" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="https://gitee.com/aiminminai" target="_blank" title="Gitee"><i class="fa fa-g"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content"><b><font color="#e66b6d">掬</font> <font color="#e66d98">水</font> <font color="#e66cc6">月</font> <font color="#cc6de6">在</font> <font        color="#9770e6">手</font> <font color="#6d93e6">，</font> <font color="#6fcde6">弄</font> <font color="#72e6b6">花</font> <font color="#72e689">香</font> <font color="#99e670">满</font> <font color="#cde670">衣</font> <p align="center"><img src="https://haiyong.site/img/img-blog.csdnimg.cn/f7384c88956d4378b72e47548e19c9f8.gif" width="50" alt="mao"></p> <p align="center">微信号：phone_15902953075</p> <p align="center">QQ号：1317957931</p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81WPF%E4%B8%AD%E7%9A%84%E5%A4%8D%E7%94%A8%E7%B2%BE%E7%A5%9E%EF%BC%9A%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF-Inline-styles-and-templates"><span class="toc-number">2.</span> <span class="toc-text">一、WPF中的复用精神：内联样式和模板 Inline styles and templates</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%B5%8C%E5%85%A5%E5%BC%8F%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF%E7%9A%84%E9%99%90%E5%88%B6-Limitations-of-embedded-styles-and-templates%EF%BC%9A"><span class="toc-number">2.1.</span> <span class="toc-text">1.嵌入式样式和模板的限制 Limitations of embedded styles and templates：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A3%B0%E6%98%8E%E6%A0%B7%E5%BC%8F%E7%9A%84%E6%9C%80%E5%B8%B8%E8%A7%81%E6%96%B9%E6%B3%95%E6%98%AF%E5%9C%A8-XAML-%E6%96%87%E4%BB%B6%E7%9A%84-Resources-%E9%83%A8%E5%88%86%E4%B8%AD%E5%A3%B0%E6%98%8E%E4%B8%BA%E8%B5%84%E6%BA%90%E3%80%82-%E7%94%B1%E4%BA%8E%E6%A0%B7%E5%BC%8F%E6%98%AF%E4%B8%80%E7%A7%8D%E8%B5%84%E6%BA%90%EF%BC%8C%E5%A3%B0%E6%98%8E%E6%A0%B7%E5%BC%8F%E7%9A%84%E4%BD%8D%E7%BD%AE%E4%BC%9A%E5%BD%B1%E5%93%8D%E6%A0%B7%E5%BC%8F%E7%9A%84%E5%BA%94%E7%94%A8%E8%8C%83%E5%9B%B4%E3%80%82-%E4%BE%8B%E5%A6%82%EF%BC%8C%E5%A6%82%E6%9E%9C%E5%9C%A8%E5%BA%94%E7%94%A8%E5%AE%9A%E4%B9%89-XAML-%E6%96%87%E4%BB%B6%E7%9A%84%E6%A0%B9%E5%85%83%E7%B4%A0%E4%B8%AD%E5%A3%B0%E6%98%8E%E6%A0%B7%E5%BC%8F%EF%BC%8C%E5%88%99%E8%AF%A5%E6%A0%B7%E5%BC%8F%E5%8F%AF%E4%BB%A5%E5%9C%A8%E5%BA%94%E7%94%A8%E4%B8%AD%E7%9A%84%E4%BB%BB%E4%BD%95%E4%BD%8D%E7%BD%AE%E4%BD%BF%E7%94%A8%E3%80%82-%E4%BE%8B%E5%A6%82%EF%BC%8C%E4%BB%A5%E4%B8%8B-XAML-%E4%BB%A3%E7%A0%81%E4%B8%BA-TextBlock-%E5%A3%B0%E6%98%8E%E4%BA%86%E4%B8%A4%E4%B8%AA%E6%A0%B7%E5%BC%8F%EF%BC%8C%E4%B8%80%E4%B8%AA%E8%87%AA%E5%8A%A8%E5%BA%94%E7%94%A8%E4%BA%8E%E6%89%80%E6%9C%89-TextBlock-%E5%85%83%E7%B4%A0%EF%BC%8C%E5%8F%A6%E4%B8%80%E4%B8%AA%E5%88%99%E5%BF%85%E9%A1%BB%E6%98%BE%E5%BC%8F%E5%BC%95%E7%94%A8%E3%80%82"><span class="toc-number">2.2.</span> <span class="toc-text">声明样式的最常见方法是在 XAML 文件的 Resources 部分中声明为资源。 由于样式是一种资源，声明样式的位置会影响样式的应用范围。- 例如，如果在应用定义 XAML 文件的根元素中声明样式，则该样式可以在应用中的任何位置使用。- 例如，以下 XAML 代码为 TextBlock 声明了两个样式，一个自动应用于所有 TextBlock 元素，另一个则必须显式引用。1234567891011121314151617181920212223242526272829303132&lt;Window.Resources&gt;    &lt;!-- .... other resources .... --&gt;    &lt;!--A Style that affects all TextBlocks--&gt;    &lt;Style TargetType&#x3D;&quot;TextBlock&quot;&gt;        &lt;Setter Property&#x3D;&quot;HorizontalAlignment&quot; Value&#x3D;&quot;Center&quot; &#x2F;&gt;        &lt;Setter Property&#x3D;&quot;FontFamily&quot; Value&#x3D;&quot;Comic Sans MS&quot;&#x2F;&gt;        &lt;Setter Property&#x3D;&quot;FontSize&quot; Value&#x3D;&quot;14&quot;&#x2F;&gt;    &lt;&#x2F;Style&gt;        &lt;!--A Style that extends the previous TextBlock Style with an x:Key of TitleText--&gt;    &lt;Style BasedOn&#x3D;&quot;{StaticResource {x:Type TextBlock}}&quot;        TargetType&#x3D;&quot;TextBlock&quot;        x:Key&#x3D;&quot;TitleText&quot;&gt;        &lt;Setter Property&#x3D;&quot;FontSize&quot; Value&#x3D;&quot;26&quot;&#x2F;&gt;        &lt;Setter Property&#x3D;&quot;Foreground&quot;&gt;            &lt;Setter.Value&gt;                &lt;LinearGradientBrush StartPoint&#x3D;&quot;0.5,0&quot; EndPoint&#x3D;&quot;0.5,1&quot;&gt;                    &lt;LinearGradientBrush.GradientStops&gt;                        &lt;GradientStop Offset&#x3D;&quot;0.0&quot; Color&#x3D;&quot;#90DDDD&quot; &#x2F;&gt;                        &lt;GradientStop Offset&#x3D;&quot;1.0&quot; Color&#x3D;&quot;#5BFFFF&quot; &#x2F;&gt;                    &lt;&#x2F;LinearGradientBrush.GradientStops&gt;                &lt;&#x2F;LinearGradientBrush&gt;            &lt;&#x2F;Setter.Value&gt;        &lt;&#x2F;Setter&gt;    &lt;&#x2F;Style&gt;&lt;&#x2F;Window.Resources&gt;&#x2F;&#x2F;如何使用上面声明的样式&lt;StackPanel&gt;    &lt;TextBlock Style&#x3D;&quot;{StaticResource TitleText}&quot; Name&#x3D;&quot;textblock1&quot;&gt;My Pictures&lt;&#x2F;TextBlock&gt;    &lt;TextBlock&gt;Check out my new pictures!&lt;&#x2F;TextBlock&gt;&lt;&#x2F;StackPanel&gt;</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E4%B8%80%E8%88%AC%E7%9A%84-Windows-Presentation-Foundation-WPF-%E7%BC%96%E7%A8%8B%E6%A8%A1%E5%9E%8B%E5%8E%9F%E5%88%99"><span class="toc-number">2.3.</span> <span class="toc-text">2.一般的 Windows Presentation Foundation (WPF) 编程模型原则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-ControlTemplate"><span class="toc-number">2.4.</span> <span class="toc-text">3.ControlTemplate</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-TemplateBinding"><span class="toc-number">2.5.</span> <span class="toc-text">4.TemplateBinding</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-ContentControl-and-ItemsControl"><span class="toc-number">2.6.</span> <span class="toc-text">5.ContentControl and ItemsControl</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E4%B8%80%E4%B8%AA%E7%BB%91%E5%AE%9A%E5%88%B0%E7%85%A7%E7%89%87%E5%88%97%E8%A1%A8%E7%9A%84-ListBox-%E6%8E%A7%E4%BB%B6%E3%80%82"><span class="toc-number">2.7.</span> <span class="toc-text">6.一个绑定到照片列表的 ListBox 控件。</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-PropertyTrigger"><span class="toc-number">2.8.</span> <span class="toc-text">7.PropertyTrigger</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-%E8%A7%86%E8%A7%89%E7%8A%B6%E6%80%81-visual-status"><span class="toc-number">2.9.</span> <span class="toc-text">8.视觉状态 visual status</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E4%BE%8B%E5%AD%90%EF%BC%9A%E4%B8%BA-Button-%E6%8E%A7%E4%BB%B6%E5%88%9B%E5%BB%BA%E6%96%B0%E7%9A%84-ControlTemplate"><span class="toc-number">3.</span> <span class="toc-text">二、例子：为 Button 控件创建新的 ControlTemplate</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E4%BD%95%E6%97%B6%E5%88%9B%E5%BB%BA-ControlTemplate-When-to-create-ControlTemplate%EF%BC%9A%E8%A6%81%E8%87%AA%E5%AE%9A%E4%B9%89%E8%AE%BE%E7%BD%AE%E6%8E%A7%E4%BB%B6%E4%B8%AD%E5%85%B6%E4%BB%96%E5%B1%9E%E6%80%A7%E6%97%A0%E6%B3%95%E5%AE%9E%E7%8E%B0%E7%9A%84%E6%8E%A7%E4%BB%B6%E5%A4%96%E8%A7%82%E6%97%B6%EF%BC%8C%E5%88%99%E5%88%9B%E5%BB%BA-ControlTemplate"><span class="toc-number">3.1.</span> <span class="toc-text">1.何时创建 ControlTemplate When to create ControlTemplate：要自定义设置控件中其他属性无法实现的控件外观时，则创建 ControlTemplate</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E5%88%9B%E5%BB%BA%E6%96%B0%E7%9A%84-WPF-%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%EF%BC%8C%E5%9C%A8-MainWindow-xaml%EF%BC%88%E6%88%96%E9%80%89%E6%8B%A9%E7%9A%84%E5%85%B6%E4%BB%96%E7%AA%97%E5%8F%A3%EF%BC%89%E7%9A%84-%E5%85%83%E7%B4%A0%E4%B8%AD%E8%AE%BE%E7%BD%AE%E4%BB%A5%E4%B8%8B%E5%B1%9E%E6%80%A7"><span class="toc-number">3.2.</span> <span class="toc-text">2.创建新的 WPF 应用程序，在 MainWindow.xaml（或选择的其他窗口）的  元素中设置以下属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E5%88%9B%E5%BB%BA-ControlTemplate"><span class="toc-number">3.3.</span> <span class="toc-text">3.创建 ControlTemplate</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81Windows-Presentation-Foundation-WPF-%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A%E5%8A%9F%E8%83%BD"><span class="toc-number">4.</span> <span class="toc-text">三、Windows Presentation Foundation (WPF) 数据绑定功能</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B7%A8%E4%BA%BA%E7%9A%84%E8%82%A9%E8%86%80"><span class="toc-number">5.</span> <span class="toc-text">巨人的肩膀</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/03/28/%E9%A1%B9%E7%9B%AE%E5%BC%80%E5%8F%91%E6%80%BB%E7%BB%93%E4%B9%8BC-%E5%86%99%E4%BA%86%E4%B8%80%E4%B8%AA%E4%B8%8A%E4%BD%8D%E6%9C%BA%E9%A1%B5%E9%9D%A2%EF%BC%8C%E8%BD%AF%E4%BB%B6%E5%8D%A1%E6%AD%BB%E4%BA%86%EF%BC%9F%EF%BC%9F%EF%BC%9F/" title="项目开发总结之C#写了一个上位机页面，软件卡死了？？？">项目开发总结之C#写了一个上位机页面，软件卡死了？？？</a><time datetime="2024-03-28T15:41:40.000Z" title="Created 2024-03-28 23:41:40">2024-03-28</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/03/27/Golang-web-http%E6%A1%86%E6%9E%B6/" title="Golang-web/http框架">Golang-web/http框架</a><time datetime="2024-03-27T15:27:12.000Z" title="Created 2024-03-27 23:27:12">2024-03-27</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/03/24/Golang-%E7%BB%83%E5%9F%BA%E6%9C%AC%E5%8A%9F%EF%BC%9A%E6%89%8E%E9%A9%AC%E6%AD%A5%E3%80%81%E7%BB%83%E6%8A%95%E7%AF%AE/" title="Golang-练基本功：扎马步、练投篮、练运球">Golang-练基本功：扎马步、练投篮、练运球</a><time datetime="2024-03-24T15:42:23.000Z" title="Created 2024-03-24 23:42:23">2024-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/03/15/%E8%83%BD%E4%B8%8D%E8%83%BD%E7%BB%99%E5%88%AB%E4%BA%BA%E4%B8%80%E4%B8%AA%E4%B8%8A%E6%89%8B%E5%B0%B1%E8%83%BD%E7%9C%8B%E6%87%82%E5%B0%B1%E8%83%BD%E7%94%A8%E7%9A%84%E4%B8%89%E5%B1%82%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%EF%BC%88%E4%BB%A3%E7%A0%81%EF%BC%89%E6%A8%A1%E5%9E%8B%E5%91%A2%EF%BC%9F/" title="能不能给别人一个上手就能看懂就能用的三层神经网络（代码）模型呢？">能不能给别人一个上手就能看懂就能用的三层神经网络（代码）模型呢？</a><time datetime="2024-03-14T16:23:21.000Z" title="Created 2024-03-15 00:23:21">2024-03-15</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/aiminminai/2024/02/28/%E6%89%93%E7%A0%B4%E7%A0%82%E9%94%85%E9%97%AE-WPF%E5%8E%9F%E7%90%86-03-WPF%E6%A0%B7%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%92%8C%E6%A8%A1%E6%9D%BF%E5%8C%96%E3%80%81%E5%86%85%E8%81%94%E6%A0%B7%E5%BC%8F%E5%92%8C%E6%A8%A1%E6%9D%BF/" title="打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板">打破砂锅问-WPF原理-03-WPF样式设置和模板化、内联样式和模板</a><time datetime="2024-02-28T15:10:28.000Z" title="Created 2024-02-28 23:10:28">2024-02-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Hu hb</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="chat-btn" type="button" title="Chat"><i class="fas fa-sms"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/aiminminai/js/utils.js"></script><script src="/aiminminai/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><script>(() => {
  const isChatBtn = true
  const isChatHideShow = true

  if (isChatBtn) {
    const close = () => {
      Chatra('minimizeWidget')
      Chatra('hide')
    }

    const open = () => {
      Chatra('openChat', true)
      Chatra('show')
    }

    window.ChatraSetup = {
      startHidden: true
    }
  
    window.chatBtnFn = () => {
      const isShow = document.getElementById('chatra').classList.contains('chatra--expanded')
      isShow ? close() : open()
    }
  } else if (isChatHideShow) {
    window.chatBtn = {
      hide: () => {
        Chatra('hide')
      },
      show: () => {
        Chatra('show')
      }
    }
  }

  (function(d, w, c) {
    w.ChatraID = 't3Zijv5iKg5Z5HT6v'
    var s = d.createElement('script')
    w[c] = w[c] || function() {
        (w[c].q = w[c].q || []).push(arguments)
    }
    s.async = true
    s.src = 'https://call.chatra.io/chatra.js'
    if (d.head) d.head.appendChild(s)
  })(document, window, 'Chatra')

})()</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>